//
//  LRVisitorView.swift
//  WeiboX
//
//  Created by admin on 01/12/2016.
//  Copyright © 2016 iOS_Objective-C. All rights reserved.
//

import UIKit
import SnapKit

protocol LRVisitorViewDelegate: NSObjectProtocol {
	func accountBtnDidSelected()
}

class LRVisitorView: UIView {
	
	var callBack: (() -> ())?
	
	weak var delegate: LRVisitorViewDelegate?
	
	override init(frame: CGRect) {
		super.init(frame: frame)
		
		setupUI()
	}
	
	lazy var cycleImgV = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
	lazy var maskImgV = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
	lazy var iconImgV = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
	
	lazy var infoLabel: UILabel = {
		let label = UILabel()
		label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
		label.textColor = UIColor.darkGray
		label.font = UIFont.systemFont(ofSize: 13)
		label.textAlignment = .center
		label.numberOfLines = 0
		return label
	}()
	
	lazy var registerBtn: UIButton = {
		let btn = UIButton()
		
		btn.addTarget(self, action: #selector(accountBtnAction(sender:)), for: .touchUpInside)
		
		btn.titleLabel?.text = "register"
		btn.setTitle("register", for: .normal)
		btn.setTitleColor(UIColor.darkGray, for: .normal)
		btn.setTitleColor(UIColor.orange, for: .highlighted)
		btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
		btn.adjustsImageWhenHighlighted = false
		return btn
	}()
	
	lazy var loginBtn: UIButton = {
		let btn = UIButton()
		
		btn.addTarget(self, action: #selector(accountBtnAction(sender:)), for: .touchUpInside)
		
		btn.titleLabel?.text = "login"
		btn.setTitle("login", for: .normal)
		btn.setTitleColor(UIColor.darkGray, for: .normal)
		btn.setTitleColor(UIColor.orange, for: .highlighted)
		btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
		btn.adjustsImageWhenHighlighted = false
		return btn
	}()
	
	@objc private func accountBtnAction(sender: UIButton) {
		print(sender.titleLabel!.text!)
		delegate?.accountBtnDidSelected()
		callBack?()
	}
	
	private func setupUI() {
		addSubview(cycleImgV)
		addSubview(maskImgV)
		addSubview(iconImgV)
		addSubview(infoLabel)
		addSubview(registerBtn)
		addSubview(loginBtn)
		
		cycleImgV.snp.makeConstraints { (make) in
			make.center.equalToSuperview()
		}
		
		maskImgV.snp.makeConstraints { (make) in
			make.center.equalTo(cycleImgV)
		}
		
		iconImgV.snp.makeConstraints { (make) in
			make.center.equalTo(cycleImgV)
		}
		
		infoLabel.snp.makeConstraints { (make) in
			make.top.equalTo(cycleImgV.snp.bottom)
			make.width.equalTo(224)
			make.centerX.equalTo(iconImgV)
		}
		
		registerBtn.snp.makeConstraints { (make) in
			make.left.equalTo(infoLabel)
			make.top.equalTo(infoLabel.snp.bottom).offset(10)
			make.size.equalTo(CGSize(width: 80, height: 35))
		}
		
		loginBtn.snp.makeConstraints { (make) in
			make.trailing.equalTo(infoLabel)
			make.top.size.equalTo(registerBtn)
		}
	}
	
	private func cycleAnimation() {
		let anim = CABasicAnimation(keyPath: "transform.rotation")
		
		anim.toValue = 2 * M_PI
		anim.duration = 20
		anim.repeatCount = MAXFLOAT
		
		anim.isRemovedOnCompletion = false
		
		cycleImgV.layer.add(anim, forKey: nil)
	}
	
	func updateWidgetLayout(imgName: String?, lableTxt: String?) {
		if let name = imgName, let txt = lableTxt {
			iconImgV.image = UIImage(named: name)
			infoLabel.text = txt
			
			cycleImgV.isHidden = true
		} else {
			cycleAnimation()
		}
	}
	
	private func defaultConstraint() {
		cycleImgV.translatesAutoresizingMaskIntoConstraints = false
		
		addConstraint(NSLayoutConstraint(item: cycleImgV, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
		addConstraint(NSLayoutConstraint(item: cycleImgV, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0))
	}
	
	required init?(coder aDecoder: NSCoder) {
		fatalError("init(coder:) has not been implemented")
	}
	
}
